// import 'package:fluwx/fluwx.dart' as fluwx;
import 'package:tobias/tobias.dart' as tobias;

import '../loding.dart';
import '../log_utils.dart';
///
///@description
///支付帮助类
///@author: Murphy
///@create: 9/27/21 3:34 PM
///

///支付宝支付
///orderInfo：后台创建的订单信息
///callback：回调状态，-1：未安装支付宝，9000：支付成功，其他错误
void toAlipay(String orderInfo, Function callback) async {
  //检测是否安装支付宝
  var result = await tobias.isAliPayInstalled();

  if (!result) {
    xdShowToast('请先安装支付宝');
    callback.call(-1);
    return;
  }

  var payResult = await tobias.aliPay(orderInfo, evn: tobias.AliPayEvn.SANDBOX);

  if (payResult['result'] != null) {
    var resultStatus = payResult['resultStatus'];
    LogUtilTool.println('-->alipay-->result：${payResult['result']}');
    //6001：用户取消支付，4000：订单参数异常
    if (resultStatus == '9000') {
      xdShowToast('支付宝支付成功');
      print('支付宝支付成功');
      callback.call(9000);
    } else  {
      xdShowToast('支付宝支付失败');
      callback.call(resultStatus);
    }
  }
}

///微信支付
// void toWeixinPay(WxPayOrderInfo wxPayOrderInfo, Function callback) async {
//   var isInstalled = await fluwx.isWeChatInstalled;
//   if (!isInstalled) {
//     xdShowToast('请先安装微信');
//     callback.call(-1);
//   }
//   //监听支付回调
//   fluwx.weChatResponseEventHandler.listen((res) {
//     if (res is fluwx.WeChatPaymentResponse) {
//       if (res.isSuccessful) {
//         xdShowToast('微信支付成功');
//         callback.call(9000);
//       } else {
//         xdShowToast('微信支付失败');
//       }
//     }
//   });
//
//   //支付参数
//   String appid = wxPayOrderInfo.appid!; //wxAppID;
//   String partnerid = wxPayOrderInfo.partnerid!;
//   String prepayid = wxPayOrderInfo.prepayId!;
//   String package = wxPayOrderInfo.orderpackage!;
//   String noncestr = wxPayOrderInfo.noncestr!;
//   int timestamp = wxPayOrderInfo.timestamp!;
//   String sign = wxPayOrderInfo.sign!;
//   // LogUtil.println('-->微信支付签名：${sign}');
//   //发起支付
//   fluwx.payWithWeChat(appId: appid, partnerId: partnerid, prepayId: prepayid, packageValue: package, nonceStr: noncestr, timeStamp: timestamp, sign: sign).then((data) {
//     LogUtilTool.println("--->微信发起支付：$data");
//   });
// }
